package com.gz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gz.dao.SubjectMapper;
import com.gz.dto.PageData;
import com.gz.dto.SubjectDto;
import com.gz.entity.Subject;
import com.gz.service.SubjectService;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * (Subject)表服务实现类
 *
 * @author makejava
 * @since 2024-01-29 23:54:54
 */
@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectMapper subjectDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Subject queryById(Integer id) {
        return subjectDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param dto 筛选条件
     * @return 查询结果
     */
    @Override
    public PageData queryByPage(SubjectDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<Subject> list = subjectDao.queryAllByLimit(dto);
        PageInfo<Subject> pageInfo = new PageInfo<>(list);
        return new PageData<>(pageInfo.getTotal(), list);
    }
    /**
     * 分页查询
     *
     * @param dto 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Subject> queryAll(SubjectDto dto) {
        List<Subject> list = subjectDao.queryAll(dto);
        return list;
    }
    @Override
    public List<Subject> queryAllForIfCanEdit(SubjectDto dto) {
        List<Subject> list = subjectDao.queryAllForIfCanEdit(dto);
        return list;
    }

//    public static void main(String[] args) {
//        String str = "上海市嘉定区宝塔路138弄3号 204";
//        str = str.replaceAll("\\s", "");
//        System.out.println(str);
//    }
    /**
     * 新增数据
     *
     * @param subject 实例对象
     * @return 实例对象
     */
    @Override
    public Subject insert(Subject subject) {
        subject.setCrtDt(new Date());
        subject.setOptDt(new Date());
        subjectDao.insert(subject);
        return subject;
    }

    /**
     * 修改数据
     *
     * @param subject 实例对象
     * @return 实例对象
     */
    @Override
    public Subject update(Subject subject) {
        subject.setOptDt(new Date());
        subjectDao.update(subject);
        return queryById(subject.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return subjectDao.deleteById(id) > 0;
    }
}
